package Base.多线程同步.同步代码块;

/**
 * @author 陈鹏
 */
public class Example12 {
    public static void main(String[] args) {
        SaleThread2 saleThread2=new SaleThread2();
        new Thread(saleThread2,"thread1").start();
        new Thread(saleThread2,"thread2").start();
        new Thread(saleThread2,"thread3").start();
        new Thread(saleThread2,"thread4").start();
    }
}

class SaleThread2 implements Runnable{
    private int tickets=100;
    Object lock=new Object();
    @Override
    public void run() {
        while (true){
            synchronized (lock){
                if (tickets>0){
                    try{
                        Thread.sleep(100);
                    }catch (Exception e){

                    }
                    System.out.println(Thread.currentThread().getName()+"正在发售第"+tickets--+"张票");
                }
            }
        }
    }
}
